package com.stock.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一 API 响应包装类
 * 用于封装所有接口的返回结果
 *
 * @param <T> 数据类型
 */
@Data
@Schema(description = "统一响应结果")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码,200表示成功", example = "200")
    private int code;

    @Schema(description = "返回信息", example = "成功")
    private String msg;

    @Schema(description = "返回数据")
    private T data;

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(200);
        apiResult.setMsg("成功");
        apiResult.setData(data);
        return apiResult;
    }
    public static <T> ApiResult<T> success() {
        return success(null);
    }
    public static <T> ApiResult<T> error(int code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        return apiResult;
    }
    public static <T> ApiResult<T> error(String msg) {
        return error(500, msg);
    }
    public static <T> ApiResult<T> error() {
        return error(500, "系统异常");
    }

    public static <T> ApiResult<T> error(Exception e) {
        return error(500, e.getMessage());
    }
    public static <T> ApiResult<T> error(Throwable e) {
        return error(500, e.getMessage());
    }
    public static <T> ApiResult<T> error(int code, Throwable e) {
        return error(code, e.getMessage());
    }
    public static <T> ApiResult<T> error(Throwable e, String msg) {
        return error(500, msg);
    }
    public static <T> ApiResult<T> error(int code, Throwable e, String msg) {
        return error(code, msg);
    }
    public static <T> ApiResult<T> error(int code, Exception e, String msg) {
        return error(code, msg);
    }
}
